package com.imstlife.vending.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.imstlife.vending.activity.MyApplication;
import com.imstlife.vending.bean.BaseResponse;
import com.imstlife.vending.net.RetrofitManager;
import com.imstlife.vending.net.RetrofitServer;
import com.imstlife.vending.net.SimpleObserver;
import com.imstlife.vending.sgy.logger.LocalLog;
import com.imstlife.vending.vo.GetDrinkListResponse;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by lihaifeng on 17/3/15.
 */

public class AppUtil {

    private static final String TAG = "AppUtil";
    public static String getVersionName(Context context){
        String versionName = "1.0";
        try{
           versionName =   context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
        }catch (Exception e){
            e.printStackTrace();
        }

          return versionName;
    }

    public static int getVersionCode(Context context){
         int versionCode = 1;
        try{
            versionCode =   context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionCode;
        }catch (Exception e){
            e.printStackTrace();
        }

        return versionCode;
    }


    public static List<String> getTimeList(String str1, int day){
        List<String> list = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");// 输入日期的格式
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(str1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar cal1 = new GregorianCalendar();
        cal1.setTime(date1);
        for (int i=0;i<=day;i++){
            long time = cal1.getTimeInMillis()+i*24*60*60*1000+10000;
            Date date = new Date(time);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String filePath = "/sdcard/vending/"+dateFormat.format(date)+".txt";
            File file = new File(filePath);
            if (file.exists()){
                list.add(filePath);
            }
        }
        return list;
    }

    public static int isOutTime(String str1, String str2){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");// 输入日期的格式
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(str1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date2 = null;
        try {
            date2 = simpleDateFormat.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();
        cal1.setTime(date1);
        cal2.setTime(date2);
        double dayCount = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000 * 3600 * 24);// 从间隔毫秒变成间隔天数
        return (int) dayCount;
    }

    public static void sendLog(final List<String> data, final String fileName, final String id){
        new Thread(){
            @Override
            public void run() {
                super.run();
                MultipartBody.Part photo1part2 = null;
                final String path;
                if (data.size()==0){
                    path = null;
                    photo1part2 = MultipartBody.Part.createFormData("","");
                }else{
                    path = LocalLog.getUploadFile2(data,fileName,DeviceUtil.getDeviceId(MyApplication.getInstance()));
                    if (path==null){
                        return;
                    }
                    final File file = new File(path);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    //参数1 数组名，参数2 文件名。
                    photo1part2 = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
                }


                RetrofitManager.createJsonService(RetrofitServer.class)
                        .upLoadLog("http://www.u-24.cn/WeChatServer/img/uploadLog.do",id,photo1part2)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new SimpleObserver<String>() {
                            @Override
                            public void onNext(@NonNull String s) {
                                if (path!=null){
                                    File file = new File(path);
                                    if (file.exists())
                                        file.delete();
                                }
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {
                                if (path!=null){
                                    File file = new File(path);
                                    if (file.exists())
                                        file.delete();
                                }
                            }
                        });
            }
        }.start();
    }


    public static void sendLog(final String id, final List<String> data, final String fileName, final Context context, final boolean flag){
        new Thread(){
            @Override
            public void run() {
                super.run();
                MultipartBody.Part photo1part2 = null;
                final String path;
                if (data.size()==0){
                    path = null;
                    photo1part2 = MultipartBody.Part.createFormData("","");
                }else{
                    path = LocalLog.getUploadFile2(data,fileName, DeviceUtil.getDeviceId(context));
                    if (path==null){
                        return;
                    }
                    final File file = new File(path);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    //参数1 数组名，参数2 文件名。
                    photo1part2 = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
                }

                if (flag){
                    RetrofitManager.createJsonService(RetrofitServer.class)
                            .upLoadLog(Cof.Interface.upLog2,id,photo1part2)
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribeOn(Schedulers.io())
                            .subscribe(new SimpleObserver<String>() {
                                @Override
                                public void onNext(@NonNull String s) {
                                    if (path!=null){
                                        File file = new File(path);
                                        if (file.exists())
                                            file.delete();
                                    }
                                }

                                @Override
                                public void onError(@NonNull Throwable e) {
                                    if (path!=null){
                                        File file = new File(path);
                                        if (file.exists())
                                            file.delete();
                                    }
                                }
                            });
                }else{
                    RetrofitManager.createJsonService(RetrofitServer.class)
                            .upLoadLog(Cof.Interface.upLog,photo1part2)
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribeOn(Schedulers.io())
                            .subscribe(new SimpleObserver<String>() {
                                @Override
                                public void onNext(@NonNull String s) {
                                    if (path!=null){
                                        File file = new File(path);
                                        if (file.exists())
                                            file.delete();
                                    }
                                }

                                @Override
                                public void onError(@NonNull Throwable e) {
                                    if (path!=null){
                                        File file = new File(path);
                                        if (file.exists())
                                            file.delete();
                                    }
                                }
                            });
                }

            }
        }.start();
    }
}
